/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.controller;

import com.hvtt.web.utilities.ConstClass;
import com.hvtt.web.utilities.Utilities;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import org.apache.commons.io.FileUtils;
import org.primefaces.component.selectbooleancheckbox.SelectBooleanCheckbox;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class FileManagerController implements Serializable {

    private List<File> listFiles;
    private List<File> listFileDeletes;
    private boolean checkFile = false;

    public FileManagerController() {
        getFiles();
    }
    
    public String getIconByFileType(String fileName){
        String ext=fileName.substring(fileName.lastIndexOf(".")+1);
        if(ext.toLowerCase().equalsIgnoreCase("doc")
                ||ext.toLowerCase().equalsIgnoreCase("docx")){
            return "/images/office-icon/Word-icon.png";
        }else if(ext.toLowerCase().equalsIgnoreCase("xls")
                ||ext.toLowerCase().equalsIgnoreCase("xlsx")){
            return "/images/office-icon/document-excel-icon.png";
        }else if(ext.toLowerCase().equalsIgnoreCase("ppt")
                ||ext.toLowerCase().equalsIgnoreCase("pptx")){
            return "/images/office-icon/PowerPoint-icon.png";
        }else if(ext.toLowerCase().equalsIgnoreCase("pdf")){
            return "/images/office-icon/pdf-icon.png";
        }else if(ext.toLowerCase().equalsIgnoreCase("exe")
                ||ext.toLowerCase().equalsIgnoreCase("msi")){
            return "/images/office-icon/File-EXE-icon.png";
        }else if(ext.toLowerCase().equalsIgnoreCase("zip")
                ||ext.toLowerCase().equalsIgnoreCase("rar")){
            return "/images/office-icon/document-compress-icon.png";
        }else if(ext.toLowerCase().equalsIgnoreCase("flv")
                ||ext.toLowerCase().equalsIgnoreCase("mp4")
                ||ext.toLowerCase().equalsIgnoreCase("mov")
                ||ext.toLowerCase().equalsIgnoreCase("mp3")){
            return "/images/office-icon/videos-icon.png";
        }else if(ext.toLowerCase().equalsIgnoreCase("iso")){
            return "/images/office-icon/Filetype-ISO-icon.png";
        }
        return "/Administrator/UserUpload/"+fileName;
    }
    

    /**
     * Get path with none /
     *
     * @return path
     */
    public String getLocalPath() {
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        return context.getRealPath("/Administrator/UserUpload");
    }

    /**
     * Get url path with none /
     *
     * @return path
     */
    public String getUrlUploadFiles() {
        return FacesContext.getCurrentInstance().getExternalContext().getInitParameter(ConstClass.UPLOAD_FILE_PATH);
    }

    private void getFiles() {
        File f = new File(getLocalPath());
        Collection<File> col = FileUtils.listFiles(f, null, true);
        this.getListFiles().clear();
        for (File ff : col) {
            this.getListFiles().add(ff);
        }
    }

    public void handleFileUpload(FileUploadEvent event) {
        try {
            UploadedFile file = event.getFile();
            FileUtils.copyInputStreamToFile(file.getInputstream(), new File(getLocalPath() + File.separator + Utilities.generateFileNameUpload(file.getFileName())));
            getFiles();
        } catch (IOException ex) {
            Logger.getLogger(FileManagerController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void checkDelete(AjaxBehaviorEvent event) {
        SelectBooleanCheckbox booleanCheckbox = (SelectBooleanCheckbox) event.getSource();
        File f = (File) booleanCheckbox.getAttributes().get("CurentFile");
        if (Boolean.parseBoolean(booleanCheckbox.getValue().toString())) {
            if (!this.getListFileDeletes().contains(f)) {
                this.getListFileDeletes().add(f);
            }
        } else {
            this.getListFileDeletes().remove(f);
        }
    }

    public void deleteFiles(ActionEvent event) {
        for(File f:getListFileDeletes()){
            try {
                Utilities.deleteFile(f);
            } catch (IOException ex) {
                Logger.getLogger(FileManagerController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        getFiles();
    }

    public List<File> getListFiles() {
        if (listFiles == null) {
            listFiles = new ArrayList();
        }
        return listFiles;
    }

    public void setListFiles(List<File> listFiles) {
        this.listFiles = listFiles;
    }

    public List<File> getListFileDeletes() {
        if (listFileDeletes == null) {
            listFileDeletes = new ArrayList();
        }
        return listFileDeletes;
    }

    public void setListFileDeletes(List<File> listFileDeletes) {
        this.listFileDeletes = listFileDeletes;
    }

    public boolean isCheckFile() {
        return checkFile;
    }

    public void setCheckFile(boolean checkFile) {
        this.checkFile = checkFile;
    }
}
